package dataTypes;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * @author Rodolfo
 *
 */
public class MethodParsUtil {


	public static String getToURI(Map<String,String> pMap)
	{
		return pMap.get("pn");
	}


	public static String getId(Map<String,String> pMap)
	{
		return pMap.get("id");
	}
	
	
	public static String codeMessageContent(Map<String,String> pMap)
	{
		// 
		StringBuilder xmlMesage = new StringBuilder();
		//
		Iterator<String> it = pMap.keySet().iterator();
		//
		String key;
		//
		while (it.hasNext())
		{
			key = it.next(); 
			xmlMesage.append(key+"="+pMap.get(key));
			if (it.hasNext())
				xmlMesage.append("|");
		}
		//
		return xmlMesage.toString();
	}

	public static Map<String,String> decodeMessageContent(String message)
	{
		// 
		Map<String,String> pMap = new HashMap<String, String>();
		//
		String token, key, value;
		//
		StringTokenizer stGroup = new StringTokenizer(message,"|");
	    while (stGroup.hasMoreTokens()) {
    		token = stGroup.nextToken();
			System.out.println(token);
			 
			StringTokenizer stAttribute = new StringTokenizer(token,"=");
			key = stAttribute.nextToken();
			value = stAttribute.nextToken();
			pMap.put(key, value);
	    }
		//     
		return pMap;
	}
	
	public static void main (String args[])
	{
		Map<String,String> pMap = new HashMap<String, String>();
		pMap.put("nome", "rodolfo");
		pMap.put("esposa", "linda");
		pMap.put("pai", "reges");
		pMap.put("mae", "susana");
		System.out.println(pMap);
		String code = codeMessageContent(pMap);
		System.out.println(code);
		pMap = decodeMessageContent(code);
		System.out.println(pMap);
	}
	
}
